//随机验证码
let oRandom = document.querySelector(".random");
let oLook = document.querySelector(".look");
let a = " ";
let b = "0123456789";

oLook.onclick = function () {
    a = "";
    for (let i = 0; i < 4; i++) {

        a += b.charAt(Math.random() * 9);
    }
    oRandom.innerHTML = a;
}

//验证用户名是否存在(接口)
let flag = false;
$("#txt").on("blur", function () {
    $.get('../images/php/goodsAndShoppingCart/checkUser.php', { "username": $("#txt").val() }, function (data) {
        console.log(data);
        if (data.trim() == "1") {
            $("#txt").next().html("√").css("color", "green")
            flag = true;
        } else {
            $("#txt").next().html("X").css("color", "red")
        }

    })
})

//注册页面接口
$("#btn").on("click", function () {
    $.post('../images/php/goodsAndShoppingCart/addUser.php', {
        "username": $("#txt").val(), "userpass": $("#pass").val()
    }, function (data) {
        console.log(data);
        if (flag) {
            if (data.trim() == "success") {
                alert("注册成功")
                location.href = "./index.html"
            }
            if (data.trim() == "fail") {
                alert("注册失败")
            }
        }

    })
})
